package com.netas.kekmanager.dao.impl;

import com.netas.kekmanager.dao.CommentDao;
import com.netas.kekmanager.dao.model.Comment;
import com.netas.kekmanager.dao.model.CommentPk;
import com.netas.kekmanager.dao.exceptions.CommentDaoException;
import java.util.Date;
import java.util.List;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.transaction.annotation.Transactional;

public class CommentDaoImpl extends AbstractDAO implements ParameterizedRowMapper<Comment>, CommentDao
{
	protected SimpleJdbcTemplate jdbcTemplate;

	protected DataSource dataSource;

	/**
	 * Method 'setDataSource'
	 * 
	 * @param dataSource
	 */
	public void setDataSource(DataSource dataSource)
	{
		this.dataSource = dataSource;
		jdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}

	/**
	 * Method 'insert'
	 * 
	 * @param dto
	 * @return CommentPk
	 */
	@Transactional
	public CommentPk insert(Comment dto)
	{
		jdbcTemplate.update("INSERT INTO " + getTableName() + " ( text, user_id, web_resource_id, time ) VALUES ( ?, ?, ?, ? )",dto.getText(),dto.getUserId(),dto.getWebResourceId(),dto.getTime());
		CommentPk pk = new CommentPk();
		pk.setId( jdbcTemplate.queryForInt("select last_insert_id()") );
		return pk;
	}

	/** 
	 * Updates a single row in the comment table.
	 */
	@Transactional
	public void update(CommentPk pk, Comment dto) throws CommentDaoException
	{
		jdbcTemplate.update("UPDATE " + getTableName() + " SET id = ?, text = ?, user_id = ?, web_resource_id = ?, time = ? WHERE id = ?",dto.getId(),dto.getText(),dto.getUserId(),dto.getWebResourceId(),dto.getTime(),pk.getId());
	}

	/** 
	 * Deletes a single row in the comment table.
	 */
	@Transactional
	public void delete(CommentPk pk) throws CommentDaoException
	{
		jdbcTemplate.update("DELETE FROM " + getTableName() + " WHERE id = ?",pk.getId());
	}

	/**
	 * Method 'mapRow'
	 * 
	 * @param rs
	 * @param row
	 * @throws SQLException
	 * @return Comment
	 */
	public Comment mapRow(ResultSet rs, int row) throws SQLException
	{
		Comment dto = new Comment();
		dto.setId( rs.getInt( 1 ) );
		dto.setText( rs.getString( 2 ) );
		dto.setUserId( rs.getInt( 3 ) );
		dto.setWebResourceId( rs.getInt( 4 ) );
		dto.setTime( rs.getTimestamp(5 ) );
		return dto;
	}

	/**
	 * Method 'getTableName'
	 * 
	 * @return String
	 */
	public String getTableName()
	{
		return "spring2013g3.comment";
	}

	/** 
	 * Returns all rows from the comment table that match the criteria 'id = :id'.
	 */
	@Transactional
	public Comment findByPrimaryKey(int id) throws CommentDaoException
	{
		try {
			List<Comment> list = jdbcTemplate.query("SELECT id, text, user_id, web_resource_id, time FROM " + getTableName() + " WHERE id = ?", this,id);
			return list.size() == 0 ? null : list.get(0);
		}
		catch (Exception e) {
			throw new CommentDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the comment table that match the criteria ''.
	 */
	@Transactional
	public List<Comment> findAll() throws CommentDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, text, user_id, web_resource_id, time FROM " + getTableName() + " ORDER BY id", this);
		}
		catch (Exception e) {
			throw new CommentDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the comment table that match the criteria 'user_id = :userId'.
	 */
	@Transactional
	public List<Comment> findByUser(int userId) throws CommentDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, text, user_id, web_resource_id, time FROM " + getTableName() + " WHERE user_id = ?", this,userId);
		}
		catch (Exception e) {
			throw new CommentDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the comment table that match the criteria 'web_resource_id = :webResourceId'.
	 */
	@Transactional
	public List<Comment> findByWebResource(int webResourceId) throws CommentDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, text, user_id, web_resource_id, time FROM " + getTableName() + " WHERE web_resource_id = ?", this,webResourceId);
		}
		catch (Exception e) {
			throw new CommentDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the comment table that match the criteria 'id = :id'.
	 */
	@Transactional
	public List<Comment> findWhereIdEquals(int id) throws CommentDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, text, user_id, web_resource_id, time FROM " + getTableName() + " WHERE id = ? ORDER BY id", this,id);
		}
		catch (Exception e) {
			throw new CommentDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the comment table that match the criteria 'text = :text'.
	 */
	@Transactional
	public List<Comment> findWhereTextEquals(String text) throws CommentDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, text, user_id, web_resource_id, time FROM " + getTableName() + " WHERE text = ? ORDER BY text", this,text);
		}
		catch (Exception e) {
			throw new CommentDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the comment table that match the criteria 'user_id = :userId'.
	 */
	@Transactional
	public List<Comment> findWhereUserIdEquals(int userId) throws CommentDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, text, user_id, web_resource_id, time FROM " + getTableName() + " WHERE user_id = ? ORDER BY user_id", this,userId);
		}
		catch (Exception e) {
			throw new CommentDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the comment table that match the criteria 'web_resource_id = :webResourceId'.
	 */
	@Transactional
	public List<Comment> findWhereWebResourceIdEquals(int webResourceId) throws CommentDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, text, user_id, web_resource_id, time FROM " + getTableName() + " WHERE web_resource_id = ? ORDER BY web_resource_id", this,webResourceId);
		}
		catch (Exception e) {
			throw new CommentDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the comment table that match the criteria 'time = :time'.
	 */
	@Transactional
	public List<Comment> findWhereTimeEquals(Date time) throws CommentDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, text, user_id, web_resource_id, time FROM " + getTableName() + " WHERE time = ? ORDER BY time", this,time);
		}
		catch (Exception e) {
			throw new CommentDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns the rows from the comment table that matches the specified primary-key value.
	 */
	public Comment findByPrimaryKey(CommentPk pk) throws CommentDaoException
	{
		return findByPrimaryKey( pk.getId() );
	}

}
